const axios = require('axios');

async function testAuth() {
  try {
    console.log('🔍 测试认证功能...');
    
    // 测试1: 尝试登录
    console.log('1. 测试登录...');
    const loginResponse = await axios.post('http://localhost:3001/logins', {
      username: 'admin',
      password: '123456'
    });
    
    console.log('✅ 登录成功');
    console.log('响应数据:', loginResponse.data);
    
    if (loginResponse.data.token) {
      console.log('✅ Token已生成:', loginResponse.data.token.substring(0, 20) + '...');
      
      // 测试2: 使用token访问课程API
      console.log('2. 测试课程API...');
      const coursesResponse = await axios.get('http://localhost:3001/api/courses', {
        headers: {
          'Authorization': `Bearer ${loginResponse.data.token}`
        }
      });
      
      console.log('✅ 课程API访问成功');
      console.log('课程数量:', coursesResponse.data.data?.length || 0);
      
      // 测试3: 测试排班API
      console.log('3. 测试排班API...');
      const schedulesResponse = await axios.get('http://localhost:3001/api/schedules', {
        headers: {
          'Authorization': `Bearer ${loginResponse.data.token}`
        }
      });
      
      console.log('✅ 排班API访问成功');
      console.log('排班数量:', schedulesResponse.data.data?.length || 0);
      
    } else {
      console.log('❌ 登录响应中没有token');
    }
    
    console.log('✅ 所有测试通过！');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.response?.data || error.message);
    if (error.response) {
      console.error('状态码:', error.response.status);
      console.error('响应数据:', error.response.data);
    }
  }
}

// 运行测试
testAuth();
